from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


driver = webdriver.Chrome(service=Service('D:\\myproject\\feng-huang\\1.自动化\\1.selenium\\chromedriver.exe'))
driver.maximize_window()

for i in range(1, 11):
    driver.get(f'https://www.foodtalks.cn/company/select?page={i}')
    time.sleep(5)
    # data = []
    # names = driver.find_elements(By.XPATH,
    #                              '/html/body/div[1]/div[2]/section/main/section/main/div[1]/div[2]/div[2]/div')
    # for name in names:
    #     dict = {'简称': '', '产品类别': '', '排名': '', '位置': '', '产品': '', 'url': '',
    #             }
    #     title = name.find_element(By.XPATH, './a/div[2]/h2')
    #     dict['简称'] = title.text
    #     Product_Category = name.find_element(By.XPATH, './a/div[2]/div[1]').text
    #     dict['产品类别'] = Product_Category
    #     try:
    #         Rankings = name.find_element(By.CLASS_NAME, 'rankings').text
    #     except:
    #         Rankings = ''
    #     dict['排名'] = Rankings
    #     try:
    #         Location = name.find_element(By.CLASS_NAME, 'location').text
    #     except:
    #         Location = ''
    #     dict['位置'] = Location
    #     try:
    #         Product = name.find_element(By.CLASS_NAME, 'products').text
    #     except:
    #         Product = ''
    #     dict['产品'] = Product
    #     url = name.find_element(By.XPATH, './a')
    #     dict['url'] = url.get_attribute('href')
    #     data.append(dict)
    #     title.click()
    #     time.sleep(2)
    #     driver.switch_to.window(driver.window_handles[1])
    #     driver.close()
    #     driver.switch_to.window(driver.window_handles[0])
    #     time.sleep(2)
    # print(data)
    # time.sleep(2)

    time.sleep(2)